package com.udi.project.business.utils.excel.utils;


import com.udi.common.exception.base.BaseException;
import com.udi.common.utils.StringUtils;
import com.udi.framework.web.domain.AjaxResult;
import com.udi.project.business.utils.excel.daomain.LogoExcel;
import com.udi.project.business.utils.excel.daomain.deviceClinicalExcel;
import com.udi.project.business.utils.excel.daomain.devicePackageExcel;
import com.udi.project.business.utils.excel.daomain.deviceStorageExcel;
import com.udi.project.business.utils.excel.mapper.ExcelMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 解析多个sheet的excel表格
 *
 * */
@Component
@Slf4j
public class ExcelListUtil {

    @Autowired
    private Message message;
    @Autowired
    private ExcelMapper excelMapper;

    /**FileInputStream is*/
    public AjaxResult parsing(InputStream url) throws Exception {
       // InputStream is = new FileInputStream(new File(url));
        //获取所有工作表的数量
        Workbook workbook = new XSSFWorkbook(url);
        int numOfSheet = workbook.getNumberOfSheets();

        for(int i = 0; i < numOfSheet; i++){
            Sheet sheet = workbook.getSheetAt(i);
            //标识表
            if(i==0){
                importExcel<LogoExcel> importExcel = new importExcel<LogoExcel>(LogoExcel.class);
                List<LogoExcel> list = importExcel.exce(sheet);
                for (int j = 0; j < list.size(); j++) {
                    if((list.get(j)).getDi()!=null){
                        /*根据di获取统一社会信用代码*/
                        String code = (excelMapper.getCode(list.get(j).getDi())).getTYSHXYDM();
                        System.out.println("-----------------"+code);
                        list.get(j).setTYSHXYDM(code);
                        /*必要信息的填入*/
                        (list.get(j)).setStatu("0");
                        (list.get(j)).setDel_flag("0");
                    }else {
                        //角标跳一次，使得下次循环还是当前下标
                        list.remove(j--);
                    }
                }
                log.info("标识表====="+list);
                //校验是否有重复数据
                List<String> data = new ArrayList<>();
                for (LogoExcel logoExcel:list) {
                    data.add(logoExcel.getDi());
                }
                Integer check = excelMapper.getChecklistUnique(data);
                if(check >0){
                    return AjaxResult.error("导入存在重复数据！请检查！");
                }else{
                    excelMapper.insertLogo(list);
                }
            }
            //包装标识信息申报表格
            if(i==1){
                importExcel<devicePackageExcel> importExcel = new importExcel<devicePackageExcel>(devicePackageExcel.class);
                List<devicePackageExcel> list = importExcel.exce(sheet);
                log.info("包装标识信息申报表格====="+list);
                excelMapper.insertPackage(list);
            }
            //存储或操作信息表格
            if(i==2){
                importExcel<deviceStorageExcel> importExcel = new importExcel<deviceStorageExcel>(deviceStorageExcel.class);
                List<deviceStorageExcel> list = importExcel.exce(sheet);
                log.info("存储或操作信息表格====="+list);
                excelMapper.insertStorage(list);
            }
            //临床尺寸信息
            if(i==3){
                importExcel<deviceClinicalExcel> importExcel = new importExcel<deviceClinicalExcel>(deviceClinicalExcel.class);
                List<deviceClinicalExcel> list = importExcel.exce(sheet);
                log.info("存储或操作信息表格====="+list);
                excelMapper.insertClinical(list);
            }
        }
        return AjaxResult.success("成功！");
    }

}
